
package io.gps.datatransfer;

import io.gps.datatransfer.codec.DevHandler;
import io.gps.datatransfer.codec.TCPCodecFactory;
import io.gps.datatransfer.utils.SysDef;
import org.apache.log4j.Logger;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 * tcp接收端初始化
 * @author bcs
 * @version 2.0, $Date:  2020-11-26 13:04:48 $
 */
public class TcpController {
	/**
	 * tcp接收控制端
	 */
	static NioSocketAcceptor acceptor = null;
	private Logger logger = Logger.getLogger(getClass());
	public TcpController(TCPCodecFactory codecFactory) throws Exception {
		// 初始化参数
		init(codecFactory);
	}
	DevHandler handler;
	/**
	 * 初始化tcp接收设置
	 * @param codecFactory
	 *
	 */
	private void init(TCPCodecFactory codecFactory) throws Exception  {
		
			// 初始化本地参数
		acceptor = new NioSocketAcceptor();
		acceptor.setReuseAddress(false);
		handler = new DevHandler();
		acceptor.setHandler(handler);
		acceptor.getSessionConfig().setMaxReadBufferSize(20480);
		acceptor.getSessionConfig().setKeepAlive(true);
		acceptor.getSessionConfig().setSoLinger(0);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,SysDef.SESSION_HAND_TIMEOUT); // 设置session无信息交互的情况下存活时间
		acceptor.getFilterChain().addLast("protocol",new ProtocolCodecFilter(codecFactory));
		bind();
	}

	/**
	 * 开始监听端口
	 * @throws Exception
	 */
	public void bind() throws Exception{
		acceptor.bind(new InetSocketAddress(SysDef.LOCAL_DEV_PORT));
		logger.info("TCP Listening on port " + SysDef.LOCAL_DEV_PORT);
	}

	public void reset(InetAddress address) {
		// TODO Auto-generated method stub

	}

	public void resetAll() {
		// TODO Auto-generated method stub

	}
	
}
